//
//  HLStatuseCell.swift
//  HLwWeibo
//
//  Created by hqhxu on 16/3/2.
//  Copyright © 2016年 hqhxu. All rights reserved.
//

import UIKit

import SnapKit
class HLStatuseCell: UITableViewCell {

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    var bottonTopConstain:Constraint?
    
    /// 微博 数组数据
    var statuses:HLStatuses? {
        didSet {
            originView.statuses = statuses

            // 先判断是否是被转发微博
            bottonTopConstain?.uninstall()
            if let reweetedStatuse = statuses?.retweeted_status {
                
                retweetedView.reweetedStatuse = reweetedStatuse
                
                retweetedView.hidden = false
                
                bottonView.snp_makeConstraints(closure: { (make) -> Void in
                    bottonTopConstain = make.top.equalTo(retweetedView.snp_bottom).constraint
                })
                
            } else {
                // 把被转发的微博给掩藏
                
                retweetedView.hidden = true
                
                bottonView.snp_makeConstraints(closure: { (make) -> Void in
                    bottonTopConstain = make.top.equalTo(originView.snp_bottom).constraint
                })
            }
        }
    }
    

    // 测试
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)

        
        backgroundColor = UIColor(white: 237 / 255.0, alpha: 1)
        prepareUI()
    }
    

    
    // MARK - ui界面相关操作
    private func prepareUI() {
        contentView.addSubview(originView)
        contentView.addSubview(retweetedView)
        contentView.addSubview(bottonView)
        
        
        originView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentView.snp_top).offset(10)
            
            make.leading.trailing.equalTo(contentView)
        }
        
        retweetedView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(originView.snp_bottom)
            make.leading.trailing.equalTo(contentView)
            
            // 缺少底部约束  内部约束了
//            make.size.equalTo(CGSizeMake(UIScreen.width, 200))
        }
        
        bottonView.snp_makeConstraints { (make) -> Void in
            bottonTopConstain = make.top.equalTo(retweetedView.snp_bottom).constraint
            
            make.leading.trailing.equalTo(contentView)
            make.height.equalTo(44)
        }
        
        
        
        contentView.snp_makeConstraints { (make) -> Void in
            
            make.top.leading.trailing.equalTo(self)
            
            make.bottom.equalTo(bottonView)
        }
    }
    
    
    
    lazy var originView:HLStatuseOriginalView = HLStatuseOriginalView()
    
    lazy var retweetedView:HLRetweetedStatuseView = HLRetweetedStatuseView()
    
    lazy var bottonView:HLStatuseBottonView = HLStatuseBottonView()
    
}
